<template>
    <el-dialog :title="title" :visible.sync="visible" append-to-body lock-scroll :before-close="onClose"
        :close-on-click-modal="false" @open="onOpen" width="400px">
        <el-form :model="formData" :rules="formRules" ref="form" label-width="80px" class="dialog-form"
            v-loading="loading" v-if="visible">
            <el-form-item label="分组名称" prop="name">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <!-- <el-form-item label="上级分组" prop="topNode">
                <el-select clearable v-model="formData.parentId">
                    <el-option v-for="(item, index) in treeNode" :label="item.name" :key="index" :value="item.groupId">
                    </el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="上级分组" prop="topNode">
                <el-cascader v-model="formData.parentId" :options="treeNodeList"
                    :props="{ value: 'parentId',label:'name',children:'children',expandTrigger: 'hover'}"
                    :show-all-levels="false" @change="handleChange">
                </el-cascader>
            </el-form-item>
            <!-- <el-form-item label="排序" prop="topNode">
                <el-input v-model="formData.addNode"></el-input>
            </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer text-right">
            <el-button @click="onClose">取消</el-button>
            <el-button type="primary" @click="onOK" :loading="loading">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {
        copyObj,
        isNullOrEmpty,
        treeList
    } from "@/utils";

    import {
        FileService
    } from "@/api";

    export default {
        data() {
            return {
                visible: false,
                loading: false,
                editing: false,
                title: "新增分组",
                original: {},
                formData: {
                    name: "",
                    parentId: '',
                    groupId: ''
                },
                formRules: {
                    name: [{
                        required: true,
                        message: "请输入新增的分组名称",
                        trigger: "blur",
                    }, ],
                    topNode: [{
                        required: true,
                        message: "请选择新增分组上级",
                        trigger: "blur",
                    }, ],

                },
                treeNode: [],
                treeNodeList: [],
                parentId: ''
            };
        },
        methods: {
            handleChange(e) {
                console.log(e)
                this.formData.parentId = e.slice(-1)[0]
                console.log(this.formData)
            },
            async onOpen() {
                // this.formData = Object.assign({}, this.original);
                this.loading = true;
                //这里放 打开弹框需要请求的数据方法 需要加 await
                this.loading = false;
            },
            onClose() {
                this.$refs.form.resetFields();
                this.$refs.form.clearValidate();
                this.visible = false;
                this.editing = false;
                this.loading = false;
                this.treeNode = ''
                this.formData.parentId = ''
            },
            async editRow(row) {
                console.log(row)
                this.parentId = row.parentId
                this.editing = true;
                this.visible = true;
                await this.getTree()
                this.formData.parentId = row.parentId
                this.formData.groupId = row.groupId
            },
            onOK() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        console.log(this.formData)
                        // this.saveForm();
                    }
                });
            },

            async getTree() {
                let res = await FileService.tree();
                if (res.code == 200) {
                    // let group = res.result.find(item => item.groupId == this.formData.groupId)
                    // this.formData.groupId = group? '': group.groupId
                    // this.formData.parentId = group? '': group.parentId
                    this.treeNodeList = res.result
                    console.log(this.formData)
                }
            },
            async saveForm() {
                try {
                    this.loading = true;
                    // let params = copyObj(this.formData);
                    let params = {
                        name: this.formData.name,
                        parentId: this.formData.parentId
                    }
                    //这里处理发送的数据
                    let res = await FileService.add(params);
                    if (res.code === 200) {
                        this.$message.success(res.message);
                        this.onClose();
                        // this.$emit("submit");
                    }
                } finally {
                    this.loading = false;
                }
            },
            // async getTrees() {
            //     let res = await FileService.list();
            //     let tree = await FileService.tree();
            //     this.treeNodeList = tree.result
            //     if (res.code === 200) {
            //         let obj = res.result.find(item => item.groupId == this.parentId)
            //         let parentId = isNullOrEmpty(obj) ? '1' : obj.parentId
            //         let groupId = res.result.find(item => item.parentId == this.parentId).parentId
            //         let rows = []
            //         if (parentId === '1') {
            //             res.result.forEach(item => {
            //                 if (item.parentId == groupId) {
            //                     rows.push(item)
            //                 }
            //             });
            //             this.treeNode = rows
            //         } else {
            //             res.result.forEach(item => {
            //                 if (item.parentId == parentId) {
            //                     rows.push(item)
            //                     res.result.forEach(item => {
            //                         if (item.parentId == groupId) {
            //                             rows.push(item)
            //                         }
            //                     });
            //                 }
            //             });
            //             this.treeNode = rows
            //         }

            //     } else {
            //         this.$message.error(res.message);
            //     }
            // },
        },
    };
</script>
<style>
    .row {
        display: flex;
        flex-wrap: wrap;
    }

    .expressNo-add {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
    }

    .expressNo-add-input {
        width: 200px;
    }

    .font-size {
        font-size: 24px;
        margin-left: 6px;
        cursor: pointer;
    }
</style>
